package topin.facecat.FaceCat.Service

import facecat.topin.core.FCBinary
import facecat.topin.service.FCClientService
import facecat.topin.service.FCMessage
import topin.facecat.FaceCat.UI.MainFrame

class LatestServiceClient : FCClientService() {
    var SERVICEID = 10003

    //开始推送
    var FUNCTION_SUBCODES = 0

    //取消推送
    var FUNCTION_UNSUBCODES = 1
    var m_connected = false

    /*
     * 获取套接字
     */
    var socketID = 0

    init {
        serviceID = SERVICEID
    }

    /*
     * 注册代码
     *@param requestID 请求ID
     *@param codes 代码
     */
    fun subCodes(requestID: Int, codes: String?) {
        val bw = FCBinary()
        try {
            bw.writeString(codes)
            val bytes = bw.bytes
            val ret = send(
                FCMessage(
                    serviceID, FUNCTION_SUBCODES, requestID,
                    socketID, 0, compressType, bytes.size, bytes
                )
            )
            bw.close()
        } catch (ex: Exception) {
        }
    }

    /*
     * 反注册代码
     *param requestID 请求ID
     */
    fun unSubCodes(requestID: Int) {
        val bw = FCBinary()
        try {
            bw.writeString("1")
            val bytes = bw.bytes
            val ret = send(
                FCMessage(
                    serviceID, FUNCTION_UNSUBCODES, requestID,
                    socketID, 0, compressType, bytes.size, bytes
                )
            )
            bw.close()
        } catch (ex: Exception) {
        }
    }

    /*
     * 接收消息方法
     */
    override fun onReceive(message: FCMessage) {
        super.onReceive(message)
        //sendToListener(message)
        MainFrame.m_mainFrame.m_tabFunc!!.beginInvoke(message);
    }

    override fun onClientClose(socketID: Int, localSID: Int) {
        super.onClientClose(socketID, localSID)
        m_connected = false
    }

    companion object {
        // 最新数据
        var FUNCTION_NEWDATA = 2

        /*
         * 获取最新数据
         *@param body 包体
         *@param bidyLength 包体长度
         */
        fun getLatestData(body: ByteArray?, bodyLength: Int): SecurityLatestData? {
            val br = FCBinary()
            return try {
                br.write(body, bodyLength)
                val latestData = SecurityLatestData()
                latestData.m_code = br.readString()
                latestData.m_open = br.readFloat().toDouble()
                latestData.m_lastClose = br.readFloat().toDouble()
                latestData.m_close = br.readFloat().toDouble()
                latestData.m_high = br.readFloat().toDouble()
                latestData.m_low = br.readFloat().toDouble()
                latestData.m_volume = br.readDouble()
                latestData.m_amount = br.readDouble()
                latestData.m_buyPrice1 = br.readFloat().toDouble()
                latestData.m_buyVolume1 = br.readInt()
                latestData.m_buyPrice2 = br.readFloat().toDouble()
                latestData.m_buyVolume2 = br.readInt()
                latestData.m_buyPrice3 = br.readFloat().toDouble()
                latestData.m_buyVolume3 = br.readInt()
                latestData.m_buyPrice4 = br.readFloat().toDouble()
                latestData.m_buyVolume4 = br.readInt()
                latestData.m_buyPrice5 = br.readFloat().toDouble()
                latestData.m_buyVolume5 = br.readInt()
                latestData.m_sellPrice1 = br.readFloat().toDouble()
                latestData.m_sellVolume1 = br.readInt()
                latestData.m_sellPrice2 = br.readFloat().toDouble()
                latestData.m_sellVolume2 = br.readInt()
                latestData.m_sellPrice3 = br.readFloat().toDouble()
                latestData.m_sellVolume3 = br.readInt()
                latestData.m_sellPrice4 = br.readFloat().toDouble()
                latestData.m_sellVolume4 = br.readInt()
                latestData.m_sellPrice5 = br.readFloat().toDouble()
                latestData.m_sellVolume5 = br.readInt()
                latestData.m_turnoverRate = br.readFloat().toDouble()
                latestData.m_pe = br.readFloat().toDouble()
                latestData.m_marketValue = br.readFloat().toDouble()
                latestData.m_flow = br.readFloat().toDouble()
                latestData.m_ratio = br.readFloat().toDouble()
                latestData.m_equity = br.readDouble()
                latestData.m_flowEquity = br.readDouble()
                latestData.m_upperLimit = br.readFloat().toDouble()
                latestData.m_lowerLimit = br.readFloat().toDouble()
                latestData.m_date = br.readDouble()
                br.close()
                latestData
            } catch (ex: Exception) {
                null
            }
        }
    }
}